<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/4
 * Time: 14:04
 */

namespace App\Http\Admin\Personnel\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Http\Admin\Personnel\Models\Report;
use Framework\Extend\Helpers\Upload;
use Intervention\Image\Facades\Image;
class ReportController extends Controller
{
    /**
     * 日志列表
     * @Author sxy
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function ReportList()
    {
        if (request()->isMethod('post')) {
            $oa_company_id=session('current_operation_company_id');
            if (!$oa_company_id) xThrow(ERR_CURRENT_OPERATION_COMPANY_ID_NOT_EXIST);
            $page = request('pageCurrent');
            $pageSize = request('pageSize');
            $search = request()->except(['pageCurrent', 'pageSize']);
            $searchField = [];
            $searchField[] = ['oa_company_id','=',$oa_company_id];
            if(isset($search['deportment'])) {
                $search_deportment = $search['deportment'];
                unset($search['deportment']);
            }else{
                $search_deportment='';
            }
            if(isset($search['user_name'])) {
                $search_user = $search['user_name'];
                unset($search['user_name']);
            }else{
                $search_user='';
            }
            if (!empty($search)) {
                if (isset($search['start_time'])) $searchField[] = ['created_at', ">=", strtotime($search['start_time'])];
                if (isset($search['end_time'])) $searchField[] = ['created_at', "<=", strtotime($search['end_time']) + 24 * 3600];
                unset($search['start_time']);
                unset($search['end_time']);
                foreach ($search as $k => $v) {
                    if ($v || $v == "0") $searchField[] = [$k, '=',  $v];
                }
            }

            $report = new Report();
            return $report->getReportList($page, $pageSize, $searchField,$search_deportment,$search_user);
        } else {
            return $this->view('list');
        }
    }





    /**
     * 查看日志详情
     * @Author sxy
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function ReportDetail()
    {
        try {
                $request = request()->all();
                $id = $request['id'];
                 $report = new Report();
                $reportInfo = $report->getReportInfo($id);
                return $this->view('detail')->with('reportInfo', $reportInfo);
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }

    /**
     * 下载附件
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function DownloadAttachments()
    {
        $download=request('download');
        return response()->download(public_path($download));
    }




}